package day11.reflect;

/*
 * Author: JacksonCoder
 * FileName: ReflectDemo
 * Version: 1.0.0
 * Date: 2025/4/20
 */

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射
 */

class Person {
    private String name = "default name";
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }


    public Person() {
    }

    public String getName() {
        return this.name;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String sayHello() {
        return "hello reflect";
    }

}

public class ReflectDemo {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        try {
            //获取 Class 对象的三种方式
            Class<?> clazz1 = Class.forName("day11.reflect.Person");
            Class<?> clazz2 = Person.class;
            Class<?> clazz3 = new Person().getClass();
            System.out.println("获取 Class 对象的三种方式1:" + clazz1);
            System.out.println("获取 Class 对象的三种方式2:" + clazz2);
            System.out.println("获取 Class 对象的三种方式3:" + clazz3);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        // 创建对象
        try {
            Class<?> clazz = Class.forName("day11.reflect.Person");
            Object obj = clazz.getDeclaredConstructor().newInstance(); // 无参构造
            System.out.println("无参构造: " + obj);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        // 获取和调用方法
        try {
            Class<?> clazz = Class.forName("day11.reflect.Person");
            Object obj = clazz.getDeclaredConstructor().newInstance();
            Method method = clazz.getDeclaredMethod("sayHello");
            String result = (String) method.invoke(obj);
            System.out.println("获取调用方法: " + method);
            System.out.println("获取调用方法的返回值: " + result);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //获取和修改属性
        try {
            Class<?> clazz = Class.forName("day11.reflect.Person");
            // 创建一个 Person 对象
            Object person = clazz.getDeclaredConstructor().newInstance();
            // 获取字段
            Field field = clazz.getDeclaredField("name");
            field.setAccessible(true);
            // 设置字段值
            field.set(person, "alen");
            // 正确获取字段值
            System.out.println("修改属性: " + field.get(person));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
